﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using TomShane.Neoforce.Controls;
using tactics.Interfaces;

using Microsoft.Xna.Framework.Graphics;

namespace tactics.GUI
{
    using FormatMap = Dictionary<string, string>;
    using ScreenCoords = Utility.Pair<int, int>;

    class StatusCard : IGUIParsable
    {
        Manager guiManager;

        Panel backPanel;
        ImageBox statusTexture;

        IStatusEffect statusEffect;

        FormatMap formats;
        ScreenCoords position;
        
        #region Properties

        public IStatusEffect StatusEffect
        {
            get { return statusEffect; }
        }

        public ScreenCoords Position
        {
            get { return position; }
            set { UpdatePosition(value.First, value.Second); }
        }

        public int Width
        {
            get { return backPanel.Width; }
        }

        public int Height
        {
            get { return backPanel.Height; }
        }

        public bool Visible
        {
            get { return backPanel.Visible; }
            set { backPanel.Visible = value; }
        }

        #endregion

        public StatusCard(Manager guiManagerIn, string xmlFile, IStatusEffect statusEffectIn)
        {
            InitControls(guiManagerIn, statusEffectIn);
            GUIBuilder.BuildGUI(this, guiManager, xmlFile, "StatusCard");
            SetInfoDisplay();
        }

        public void Remove()
        {
            guiManager.Remove(backPanel);
        }

        #region Initialize Controls

        private void InitControls(Manager guiManagerIn, IStatusEffect statusEffectIn)
        {
            guiManager = guiManagerIn;
            statusEffect = statusEffectIn;

            formats = new FormatMap();

            position = new ScreenCoords();

            backPanel = new Panel(guiManager);
            backPanel.Init();

            statusTexture = new ImageBox(guiManager);
            statusTexture.Init();

            backPanel.Visible = false;
        }

        #endregion

        #region Display

        private void SetInfoDisplay()
        {
            statusTexture.Image = TacticsEngine.Instance.Content.Load<Texture2D>("StatusEffects/" + statusEffect.Texture);
        }

        public void UpdatePosition(int x, int y)
        {
            position.First = x;
            position.Second = y;

            backPanel.Left = x;
            backPanel.Top = y;
        }

        #endregion
        
        #region Parsing Interface

        public Control Parse(XmlReader r, string controlName)
        {
            Control found = null;

            if (controlName == "BackPanel")
            {
                GUIBuilder.ParseControl<Panel>(r, backPanel);
                found = backPanel;
            }
            else if (controlName == "StatusTexture")
            {
                GUIBuilder.ParseControl<ImageBox>(r, statusTexture);
                found = statusTexture;
            }

            return found;
        }

        #endregion
    }
}
